package t2dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import t2bean.CardBean;
import t2jdbc.JDBCUtil;

public class CardDao extends Dao {
	private SetSql ss = new SetSql();

	public int insert(CardBean card) {
		String sql = "insert into card(cardid,name) values(?,?)";
		List<Object> list = new ArrayList<>();
		list.add(card.getCardId());
		list.add(card.getName());
		try {
			return dml(sql, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<CardBean> select(CardBean card) throws SQLException {
		List<CardBean> backList = new ArrayList<>();
		ResultSet rs = null;
		List<Object> list = new ArrayList<>();
		String sql;
		if (card != null) {
			sql = "select * from card where ";
			sql = ss.setSql(card, sql, list, 2);
		} else {
			sql = "select * from card";
		}
		try {
			rs = dql(sql, list);
			while (rs.next()) {
				CardBean card1 = new CardBean();
				card1.setId(rs.getInt("id"));
				card1.setName(rs.getString("name"));
				card1.setCardId(rs.getInt("cardid"));
				backList.add(card1);
			}
			return backList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(super.conn, super.ps, super.rs);
			super.conn = null;
			super.ps = null;
			super.rs = null;
			rs.close();
			rs = null;
		}
		return null;
	}
}
